<template>
  <div class="register">
    <div class="comeback">
      <span @click="back">返回</span>
    </div>
    <div class="container">
      <div class="title">注册</div>
        <el-form :model="registerForm" status-icon :rules="rules" ref="registerForm" label-width="100px" class="registerForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="registerForm.username"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="registerForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="emile">
            <el-input v-model="registerForm.emile"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="registerForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="registerForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item class="btn">
            <el-button type="primary" @click="submitForm('registerForm')">提交</el-button>
            <el-button @click="resetForm('registerForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    
</template>
<script>
import axios from 'axios'
export default {
  name: 'Register',
  components: {
    
  },
  data(){
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.registerForm.checkPass !== '') {
          this.$refs.registerForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.registerForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    var validatePhone = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号'));
      } else if (!/^1[3456789]\d{9}$/.test(value)) {
        callback(new Error('请输入正确的手机号'));
      } else {
        callback();
      }
    };
    var validateEmile = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入邮箱'));
      } else if (!/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value)) {
        callback(new Error('请输入正确的邮箱'));
      } else {
        callback();
      }
    };
    return {
      registerForm: {
        username:'',
        phone:'',
        emile:'',
        pass: '',
        checkPass: '',
      },
      rules: {
        pass: [
          { required: true, message: '密码不能为空'},
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '密码不能为空'},
          { validator: validatePass2, trigger: 'blur' }
        ],
        username: [
          { required: true, message: '用户名不能为空'},
          { min:2,max:16, message: '请输入2-16位字符',trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '手机不能为空'},
          { validator: validatePhone, trigger: 'blur' }
        ],
        emile: [
          { required: true, message: '邮箱不能为空'},
          { validator: validateEmile, trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    back(){
      this.$router.go(-1);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post('/user/register',{
            "email": this.registerForm.emile,
            "mobile": this.registerForm.phone,
            "name": '要什么名字',
            "password": this.registerForm.pass,
            "username": this.registerForm.username
          })
          .then(() => {
            alert('注册成功');
          })
          .catch(err => {
            alert(err.msg);
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style lang="scss">
  .comeback{
    position: absolute;
    top: 0;
    left: 0;
    width: 100px;
    height: 50px;
    margin-left: 20px;
    margin-top: 10px;
    line-height: 50px;
    font-size: 30px;
    color: #ffffff;
    border-radius: 20px;
    background-color: #409EFF;
    text-align: center;
    span{
      cursor: pointer;
    }
  }
  .container{
    position: absolute;
    width: 500px;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    // border: 1px solid #409EFF;
    border-radius: 20px;
    box-shadow: 0 0 20px 10px #b1cfec;
    .title{
      height: 50px;
      line-height: 50px;
      color: #409EFF;
      width: 100%;
      font-size: 30px;
      text-align: center;
    }
    .btn{
      margin-left: 80px;
    }
  }
</style>